﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Newtonsoft.Json;

namespace Comm
{
    public class Cart
    {
        private static string cookiename = "shop";
        /// <summary>
        /// 获取产品集合
        /// </summary>
        /// <returns></returns>
        public static List<BuyItem> getItems()
        {
            string value = CookiesHelper.GetCookieValue(cookiename);
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }

            List<BuyItem> list = (List<BuyItem>)JavaScriptConvert.DeserializeObject(value, typeof(List<BuyItem>));

            return list;
        }

        /// <summary>
        /// 保存购物车
        /// </summary>
        /// <param name="list"></param>
        public static void saveCart(List<BuyItem> list)
        {
            CookiesHelper.SetCookie(cookiename, JavaScriptConvert.SerializeObject(list), DateTime.Now.AddDays(7));
        }

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="item"></param>
        public static void Add(BuyItem item)
        {
            List<BuyItem> list = getItems();
            if (list == null || list.Count < 1)
            {
                Update(item);
                return;
            }

            BuyItem t = list.Find(delegate(BuyItem b) { return b.ProductID == item.ProductID; });
            if (t == null)
            {
                Update(item);
                return;
            }

            item.BuyNum += t.BuyNum;
            Update(item);
        }

        /// <summary>
        /// 更新产品
        /// </summary>
        /// <param name="item"></param>
        public static void Update(BuyItem item)
        {
            List<BuyItem> list = getItems();
            if (list == null || list.Count < 1)
            {
                List<BuyItem> nlist = new List<BuyItem>();
                nlist.Add(item);
                saveCart(nlist);
                return;
            }

            bool flag = true;
            foreach (BuyItem model in list)
            {
                if (model.ID == item.ID || model.ProductID == item.ProductID)
                {
                    flag = false;
                    model.BuyNum = item.BuyNum;
                }
            }
            if (flag)
            {
                list.Add(item);
            }

            saveCart(list);
        }

        /// <summary>
        /// 移除某个产品
        /// </summary>
        /// <param name="ID"></param>
        public static void Remove(string ID)
        {
            if (CookiesHelper.GetCookieValue(cookiename).Trim() == "")
            {
                return;
            }

            List<BuyItem> list = getItems();
            list.Remove(list.Find(i => i.ID == ID));

            saveCart(list);
        }

        /// <summary>
        /// 清空产品
        /// </summary>
        public static void Clear()
        {
            CookiesHelper.SetCookie(cookiename, "", DateTime.Now.AddDays(-1));
        }
    }
}
